package ofmybatis.demo.dbConfig;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"ofmybatis.demo.dao.back"} , sqlSessionFactoryRef = "backSqlSessionFactory")
public class BackDataSourceConfig {

    @Autowired
    @Qualifier("dataSourceBack")
    private DataSource dataSource;

    @Value("${mybatis.back.mapper-locations}")
    private String MAPPER_LOCATION;

    @Value("${mybatis.back.type-aliases-package}")
    private String POJO_LOCATION;

    @Bean(name = "dataSourceTransactionManagerBack")
    public DataSourceTransactionManager dataSourceTransactionManager(){
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "backSqlSessionFactory")
    public SqlSessionFactory backSqlSessionFactory() throws Exception {
//        System.out.println(MAPPER_LOCATION);
//        System.out.println(POJO_LOCATION);
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
     //   sessionFactory.setTypeAliasesPackage(POJO_LOCATION);
        return sessionFactory.getObject();

    }

}
